package problema028;

public class spiralDiagonals {
	
	static int cantPuestos;
	static int dir;
	static int[][] espiral;
	static int x;
	static int y;
	static int[] cuantosPoner;
	
	public static void main(String[] args){
		x = 500; y = 500;
		espiral = new int[1001][1001];
		cuantosPoner = new int[10000];
		dir = 1;
		int cuantosHay = 0, pos = 0;
		for(int i = 1; ; i++){
			cuantosHay += i; cuantosPoner[pos] = i; pos++;
			if(cuantosHay == 1002001) break;
			cuantosHay += i; cuantosPoner[pos] = i; pos++;
			if(cuantosHay == 1002001) break;
		}
		System.out.println(sumaDiag());
	}
	
	public static int sumaDiag(){
		int suma = 0, numAct = 1, posPoner = 0, cant = 0;
		while(numAct <= 1002001){
			while(cant < cuantosPoner[posPoner]){
				espiral[x][y] = numAct;
				switch (dir){
				case 1: y++;
						break;
				case 2: x++;
						break;
				case 3: y--;
						break;
				case 4: x--;
						break;
				}
				numAct++;
				cant++;
			}
			switch(dir){
			case 1: dir++;
					break;
			case 2: dir++;
					break;
			case 3: dir++;
					break;
			case 4: dir = 1;
					break;
			}
			cant = 0;
			posPoner++;
		}
		for(int i = 0; i < 1001; i++){
			suma += espiral[i][i];
			suma += espiral[1000-i][i];
		}
		suma--;		//resto el 1 del medio
		return suma;		
	}
	
	
	
	
}
